﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using NHibernate;
using NHibernate.Criterion;
using System.Web.UI.WebControls;
using NHibernate.Linq;
using mscook.core.domain;


namespace mscook.core.repositories
{
    public class LoginUserRepository : Repository<LoginUser>, ILoginUserRepository
    {

        public LoginUser SelectLoginUserByLoginName(string LoginName)
        {
            var q = (from x in this.Session.Query<LoginUser>() select x).Where(o => o.IsDelete == false && o.LoginName == LoginName).SingleOrDefault();
            return q;
        }

        public bool ValidateLoginUserByPsd(string LoginName,string password)
        {
            var q = (from x in this.Session.Query<LoginUser>() select x).Where(o => o.IsDelete == false && o.LoginName == LoginName
                &&o.Password==password
                );
            return q.Any();
        }

        public LoginUser SelectLoginUserByLoginNameOrEmail(string loginName, string email)
        {
            var query = (from x in this.Session.Query<LoginUser>() select x).Where(o => o.IsDelete == false &&
                (o.LoginName == loginName || o.Email == email)).SingleOrDefault();
            return query;
        }

        public bool IsNullByLoginNameOrEmail(string loginName, string email)
        {
            var query = (from x in this.Session.Query<LoginUser>() select x).Where(o => o.IsDelete == false &&
                (o.LoginName == loginName || o.Email == email));
            return !query.Any();
        }

        public bool IsNullByLoginName(string loginName)
        {
            var query = (from x in this.Session.Query<LoginUser>() select x).Where(o => o.IsDelete == false &&
                (o.LoginName == loginName));
            return !query.Any();
        }

        public bool IsNullByEmail(string email)
        {
            var query = (from x in this.Session.Query<LoginUser>() select x).Where(o => o.IsDelete == false &&
                (o.Email == email));
            return !query.Any();
        }

        public IList<LoginUser> SelectLimitLoginUserByType(bool isAnonymous, int start, int limit, out long total, System.Linq.Expressions.Expression<Func<LoginUser, bool>> expression)
        {
            var query = (from x in this.Session.Query<LoginUser>() select x).Where(o => o.IsDelete == false &&
               o.IsAnonymous == isAnonymous

                ).Where(expression).OrderBy(o=>o.UserRole);
            total = query.Count();
            return query.Skip(start).Take(limit).ToList();
        }

        public IList<LoginUser> SelectLimitSystemUser(bool isVip, int start, int limit, out long total, System.Linq.Expressions.Expression<Func<LoginUser, bool>> expression)
        {
            var query = (from x in this.Session.Query<LoginUser>() select x).Where(o => o.IsDelete == false &&
               o.IsAnonymous == true
                ).Where(expression).OrderByDescending(o=>o.CreateTime);
               

            total = query.Count();
            return query.Skip(start).Take(limit).ToList();
        }

        public LoginUser UserLogOnSelectByLoginNameOrEmail(string InputValue)
        {
            return (from x in this.Session.Query<LoginUser>() select x).Where
                (o => o.IsDelete == false && o.LoginName == InputValue || o.Email == InputValue).SingleOrDefault();
        }


    }
}
